	%% @author oberfreak
%% @doc @todo Add description to engine_serv.


-module(engine).

%% ====================================================================
%% API functions
%% ====================================================================
-compile(export_all).

-record(state, {configs}). %% current config list

start() ->
    register(?MODULE, Pid=spawn(?MODULE, init, [])),
    Pid.

start_link() ->
    register(?MODULE, Pid=spawn_link(?MODULE, init, [])),
    Pid.

terminate() ->
    ?MODULE ! shutdown.

init() ->
    %% Loading events from a static file could be done here.
    %% You would need to pass an argument to init (maybe change the functions
    %% start/0 and start_link/0 to start/1 and start_link/1) telling where the
    %% resource to find the events is. Then load it from here.
    %% Another option is to just pass the event straight to the server
    %% through this function.
    loop(#state{configs=orddict:new()}).

loop(S=#state{}) ->
	receive
		{'DOWN', Ref, process, _Pid, _Reason} ->
            loop(S#state{configs=orddict:erase(Ref, S#state.configs)});
		shutdown ->
            exit(shutdown);
        code_change ->
            ?MODULE:loop(S);
        {Pid, debug} -> %% used as a hack to let me do some unit testing
            Pid ! S,
            loop(S);
        Unknown ->
            io:format("Unknown message: ~p~n",[Unknown]),
            loop(S)
    end.

%% ====================================================================
%% Internal functions
%% ====================================================================


